//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Xml;
using System.Collections;
using CommunityServer.Components;
using CommunityServer.Configuration;

namespace CommunityServer.Blogs.Components
{
	/// <summary>
	/// This CS Job sends email notifications to blog owners when the number of moderated feedback items for a blog
	/// exceeds the ModeratedFeedbackNotificationThreshold property value.
	/// </summary>
	public class ModeratedFeedbackNotificationJob : IJob
	{

		public ModeratedFeedbackNotificationJob()
		{
		}

	    public void Execute(XmlNode node)
	    {
			SiteSettingsManager.IterateSiteSettings(new SiteSettingsListIterator(ModeratedFeedbackNotificationJob.SendModeratedFeedbackNotifications));
	    }

		public static void SendModeratedFeedbackNotifications(int settingsID)
	    {
			ArrayList blogs = Weblogs.GetWeblogs(true, true, false);
			
			foreach (Weblog blog in blogs)
			{
				bool sendNotification = false;
				
				// Check if a notification should be (or have been) sent
				if (blog.PostsToModerate > blog.ModeratedFeedbackNotificationThreshold)
					sendNotification = true;

				// Only send the notification if one has not already been sent recently.
				// Update the blog flag to remember this
				if (blog.ModeratedFeedbackNotificationSent != sendNotification)
				{
					if (sendNotification)
						WeblogEmails.WeblogFeedbackModerationThresholdNotification(blog);

					blog.ModeratedFeedbackNotificationSent = sendNotification;
					Weblogs.Update(blog);
				}			
				
			}
	    
	    }

	}
}
